<?php

declare(strict_types=1);

/**
 * 文章信息数据访问类
 * 作者 薛原
 * 日期 2025-10-17
 */

namespace App\Repository;
use App\Model\Articles;
use Hyperf\Di\Annotation\Inject;

class ArticleRepository
{
    #[Inject]
    private Articles $articlesModel;

    /**
     * 获取全部文章
     * @param int $size
     * @return array
     */
    public function getList($size = 20)
    {
        return $this->articlesModel->whereRaw("1=1")->select([
            "id","title",
            "summary","cover_image",
            "view_count","like_count",
            "comment_count","reading_time",
            "status","user_id"
        ])->paginate(intval($size))->jsonSerialize();
    }
    /**
     * 数据访问层一律返回数组格式
     * 根据用户id获取文章
     * @param string $user_id
     * @param int $size
     * @return array
     */
    public function getListByUserId(string $user_id, $size = 20)
    {
        return $this->articlesModel->where("user_id",$user_id)->select([
            "id","title",
            "summary","cover_image",
            "view_count","like_count",
            "comment_count","reading_time",
            "status","user_id"
        ])->paginate(intval($size))->jsonSerialize();
    }

    /**
     * 根据id取数据
     * @param $id
     * @param bool $with_content
     * @return array
     */
    public function getRowById($id, $with_content = false)
    {
        $fileds = [
            "id","title",
            "summary","cover_image",
            "view_count","like_count",
            "comment_count","reading_time",
            "status","user_id","published_at","content_type"
        ];
        //增加content
        if($with_content){
            $fileds[] = "content";
        }
        $data = $this->articlesModel->where("id",$id)->select($fileds)->first();
        if($data){
            return $data->toArray();
        }else{
            return null;
        }
    }

    /**
     * 创建文章
     * @param $data
     * @param $user_id
     * @return \Hyperf\Utils\HigherOrderTapProxy|mixed|void
     */
    public function create($data, $user_id)
    {
        $obj = [];
        $obj['user_id'] = $user_id;
        $obj['title'] = $data['title'];
        $obj['user_id'] = $user_id;
        $obj['content'] = $data['content'];
        $obj['content_type'] = $data['content_type'];
        $obj['cover_image'] = $data['cover_image'];
        $obj['status'] = "draft";
        $obj['slug'] = "cache_".rand(1000,9999);
        $article = $this->articlesModel->create($obj);
        $id = $article->id;
        $this->articlesModel->where("id",$id)->update(["slug"=>"/api/v1/articles/".$id]);
        return $article->id;
    }

    /** 更新文章
     * @param $data
     * @param $article_id
     */
    public function update($data, $article_id)
    {
        $obj = [];
        $obj['title'] = $data['title'];
        $obj['content'] = $data['content'];
        $obj['cover_image'] = $data['cover_image'];
        $this->articlesModel->where("id",$article_id)->update($obj);
    }

    /**
     * 删除文章
     * @param $article_id
     */
    public function deleteById($article_id)
    {
        $this->articlesModel->where("id",$article_id)->delete();
    }

    /**
     * 提交审核
     */
    public function submit($article_id)
    {
        $this->articlesModel->where("id",$article_id)->update(["status"=>"review"]);
    }

    /**
     * 更改状态
     * @param $article_id
     * @param $status
     */
    public function changeStatus($article_id, $status)
    {
        $obj = [];
        $obj['status'] = $status;
        if($status == "published"){
            $obj['published_at'] = date("Y-m-d H:i:s");
        }
        $this->articlesModel->where("id",$article_id)->update($obj);
        return $obj;
    }
}